﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows;
using DataProcessing;
namespace Gene.Render
{
    public class XZPlane : Gene.Render.BackgroudPlane
    {
        public const double X_LEFT = -1.11;
        public const double X_RIGHT = 1.11;
        public double Z_FAR = -1.32;
        public const double Z_NEAR = 1.32;
        public const double Y = -0.1;
        public const int MARKER_COUNT = 10;
        public const int CHROME_PLAEN_INTERVAL = 29;//各chrome平面间距
        public const int CHROME_TEXT_PIXEL = 5;
        public const int MAKRER_PIXEL = 5;
        public const int CHROME_PLANE_FIRST_Z=40;
        public int Z_OF_X_AXIS = 50;
        protected override Point3DCollection BuildPositions()
        {

            Z_FAR = Math.Min(-1.32, Scene.ZOfPlane(Settings.Instance.ChromeCount - 1));
            Point3DCollection myPositionCollection = new Point3DCollection();
            myPositionCollection.Add(new Point3D(X_LEFT, Y, Z_FAR));
            myPositionCollection.Add(new Point3D(X_LEFT, Y, Z_NEAR));
            myPositionCollection.Add(new Point3D(X_RIGHT, Y, Z_NEAR));
            myPositionCollection.Add(new Point3D(X_RIGHT, Y, Z_FAR));

            return myPositionCollection;

        }

        protected override void BuildBackgroundDrawer()
        {


            BK_IMG_HEIGHT = ARROW_RADIUS * 2 + (CHROME_PLAEN_INTERVAL * Settings.Instance.ChromeCount - 1);

            Z_OF_X_AXIS = Z_OF_X_AXIS * BK_IMG_HEIGHT / STANDARD_BK_IMG_HEIGHT;
                
            Image = new RenderTargetBitmap(BK_IMG_WIDTH, BK_IMG_HEIGHT, 96, 96, PixelFormats.Pbgra32);
            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            {

                Color fillingColor = new Color();
                fillingColor = Colors.LightGreen;
                
                SolidColorBrush brush = new SolidColorBrush(fillingColor);
                Pen drawingPen = new Pen(brush, BRUSH_THICKNESS);
                //画横轴
                drawingContext.DrawLine(drawingPen, new Point(0, BK_IMG_HEIGHT - Z_OF_X_AXIS), new Point(BK_IMG_WIDTH - ARROW_LENGTH, BK_IMG_HEIGHT - Z_OF_X_AXIS));
                DrawArrowOfX(drawingContext, brush );

                //画纵轴
                drawingContext.DrawLine(drawingPen, new Point(Z_START, BK_IMG_HEIGHT), new Point(Z_START, 0));
                //z轴的箭头
               //drawingContext.DrawLine(new Pen(brush, BRUSH_THICKNESS), new Point(0, ARROW_RADIUS), new Point(ARROW_RADIUS, 0));
               // drawingContext.DrawLine(new Pen(brush, BRUSH_THICKNESS), new Point(ARROW_RADIUS * 2, ARROW_RADIUS), new Point(ARROW_RADIUS, 0));

             
            }
            Image.Render(drawingVisual);

            Drawer = new ImageBrush(Image);
        }

        private void DrawArrowOfX(DrawingContext drawingContext, SolidColorBrush brush)
        {
            PathGeometry triangleGeometry = new PathGeometry();
             //x轴的箭头

            int Y_POS = BK_IMG_HEIGHT - Z_OF_X_AXIS;
            PathFigure figure = new PathFigure();
            figure.StartPoint = new Point(BK_IMG_WIDTH - ARROW_LENGTH, Y_POS - ARROW_RADIUS);
            figure.IsClosed = true;
            figure.IsFilled = true;
          
            
            
            LineSegment lineDown = new LineSegment();
            lineDown.Point = new Point(BK_IMG_WIDTH - 1, Y_POS);

            LineSegment lineUp = new LineSegment();
            lineUp.Point = new Point(BK_IMG_WIDTH - ARROW_LENGTH, Y_POS + ARROW_RADIUS);
            figure.Segments.Add(lineDown);
            figure.Segments.Add(lineUp);
            triangleGeometry.Figures.Add(figure);

            Pen drawingPen = new Pen(brush, 0.1);

            drawingContext.DrawGeometry(brush, drawingPen, triangleGeometry);
        }
        const int Z_START = 50;
        public static int ChromePlaneZ(int i)
        {
            return Z_START + i * CHROME_PLAEN_INTERVAL;
        }
        public double X_COORIDNATE_TXT_LEN = 92.4;
        
        public int X_COORDINATE_BRUSH_THICKNESS = 4;
        public int X_COORDINATE_TXT_X_START = 10;
        public void drawXCoordinateText(ValueRange range)
        {
            int X_COORDINATE_TXT_Y_PIXEL=BK_IMG_HEIGHT - 30;
            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
            {
                //擦除刚才的坐标文字
                drawingContext.DrawLine(new Pen(Brushes.Black, 24), new Point(0, BK_IMG_HEIGHT - 20), new Point(47.5, BK_IMG_HEIGHT - 20));
                drawingContext.DrawLine(new Pen(Brushes.Green, 24), new Point(47.5, BK_IMG_HEIGHT - 20), new Point(52.5, BK_IMG_HEIGHT - 20));
                drawingContext.DrawLine(new Pen(Brushes.Black, 24), new Point(52.5, BK_IMG_HEIGHT - 20), new Point(BK_IMG_WIDTH, BK_IMG_HEIGHT - 20));

                //画新的坐标
                Color fillingColor = new Color();
                fillingColor = Colors.White;
                SolidColorBrush brush = new SolidColorBrush(fillingColor);

                string str;
                str = range.Start.ToString();
                FormattedText text = new FormattedText(
                        str,
                        System.Globalization.CultureInfo.GetCultureInfo("en-us"),
                        FlowDirection.LeftToRight,
                        new Typeface("Verdana"),
                        15,
                        Brushes.Yellow);
                drawingContext.DrawText(text, new Point(X_COORDINATE_TXT_X_START, X_COORDINATE_TXT_Y_PIXEL));
                for (int i = 1; i <= MARKER_COUNT; i++)
                {
                    str = (range.Start + i * (range.End - range.Start) / MARKER_COUNT).ToString();
                    text = new FormattedText(
                            str,
                            System.Globalization.CultureInfo.GetCultureInfo("en-us"),
                            FlowDirection.LeftToRight,
                            new Typeface("Verdana"),
                            15,
                            Brushes.White);

                    //corodinate text
                    drawingContext.DrawText(text, new Point(X_COORDINATE_TXT_X_START+ i * X_COORIDNATE_TXT_LEN, X_COORDINATE_TXT_Y_PIXEL));
                    //maker line
                    const int X_MAKER_LEN =6;
                    double makerX = Z_START + i * X_COORIDNATE_TXT_LEN;
                    int makerStartY =  BK_IMG_HEIGHT - ARROW_RADIUS;
                    int makerEndY = makerStartY+X_MAKER_LEN;
                    
                    drawingContext.DrawLine(new Pen(brush, X_COORDINATE_BRUSH_THICKNESS), new Point(makerX,makerStartY), new Point(makerX, makerEndY));
                }
            }
            Image.Render(drawingVisual);
            Drawer = new ImageBrush(Image);

        }


    
    }
}
